package com.dodo.blog.ui.page.publish;

import com.comvai.auth.model.LoginProperties;
import com.comvai.auth.server.AuthService;
import com.comvai.auth.servlet.AuthorizeCallbackServlet;
import com.dodo.blog.ui.component.composite.FieldSet;
import com.dodo.blog.ui.component.composite.FormRow;
import com.dodo.blog.ui.component.container.Form;
import com.dodo.blog.ui.component.input.Anchor;
import com.dodo.blog.ui.component.input.PasswordBox;
import com.dodo.blog.ui.component.input.SubmitButton;
import com.dodo.blog.ui.component.input.TextBox;
import com.dodo.blog.ui.page.AbstractPage;

import javax.inject.Inject;
import javax.servlet.http.Cookie;
import java.util.Map;

/**
 * @author <a href="mailto:pohorelec@comvai.com">Jozef Pohorelec</a>
 */
public class Login
        extends AbstractPage
{
    private static final long serialVersionUID = 1L;

    private static AuthService authService;

    @Override
    public void onRender()
    {
        setHeader( localize( "header.login" ) );

        for ( Cookie cookie : getRequest().getCookies() )
        {
            if ( cookie.getName().equals( AuthorizeCallbackServlet.COOKIE_AUTH_ERROR ) )
            {
                cookie.setMaxAge( 0 );
                getResponse().addCookie( cookie );

                error( localize( "error." + cookie.getValue() ) );

                break;
            }
        }

        // get login properties
        LoginProperties loginProperties = authService.getLoginProperties( getRequest().getRequestURL().toString().replace( getRequest().getRequestURI(), "" ) );

        // set login properties to session
        getRequest().getSession().setAttribute( AuthorizeCallbackServlet.SESSION_LOGIN_PROPERTIES_ATTRIBUTE, loginProperties );

        // render login provider panel
        add( renderProvidersPanel( loginProperties ) );

        // render classic login panel
        add( renderLoginPanel( loginProperties ) );
    }

    private FieldSet renderProvidersPanel( LoginProperties loginProperties )
    {
        FieldSet providersFieldSet = new FieldSet( localize( "title.singleSignInLogin" ) );
        providersFieldSet.setClassName( "login-providers-panel" );

        for ( Map.Entry<String, String> provider : loginProperties.getProviders().entrySet() )
        {
            String providerKey = provider.getKey();
            Anchor a = new Anchor( providerKey, localize( "label." + providerKey ), provider.getValue() );
            a.setClassName( "login-provider" );
            a.addClassName( providerKey );
            providersFieldSet.add( a );
        }

        return providersFieldSet;
    }

    private FieldSet renderLoginPanel( LoginProperties loginProperties )
    {
        FieldSet classicLoginFieldSet = new FieldSet( localize( "title.classicLogin" ) );
        classicLoginFieldSet.setClassName( "login-classic-panel" );

        Form form = new Form( loginProperties.getLoginServletPath() );
        classicLoginFieldSet.add( form );

        form.add( new FormRow( localize( "label.login" ), new TextBox( loginProperties.getLoginParamName() ) ) );
        form.add( new FormRow( localize( "label.password" ), new PasswordBox( loginProperties.getLoginParamPassword() ) ) );
        form.add( new SubmitButton( "login", localize( "button.login" ) ) );

        return classicLoginFieldSet;
    }

    @Inject
    public void setAuthService( AuthService authService )
    {
        Login.authService = authService;
    }
}